package com.ynunicom.dc.dingdingcontractapp.utils;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;

import java.util.Map;

/**
 * @author: jinye.Bai
 * @date: 2020/5/22 15:54
 */
public class HttpEntityFactory<T> {

    private HttpHeaders httpHeaders;

    private HttpEntity httpEntity;

    private  T body;

    HttpEntityFactory(){
        this.httpHeaders = new HttpHeaders();
    }

    /**
     * 此方法会覆盖headers里面已有的参数，新建没有的参数
     * @param key
     * @param value
     * @return
     */
    public HttpEntityFactory setHeader(String key,String value){
        httpHeaders.set(key,value);
        return this;
    }

    public HttpEntityFactory setHeader(HttpHeaders in){
        httpHeaders = in;
        return this;
    }

    public HttpEntityFactory setHeader(Map<String,String> map){
        httpHeaders.setAll(map);
        return this;
    }

    public HttpEntityFactory setBody(T in){
        this.body = in;
        return this;
    }

    public HttpEntity build(){
        if(this.body == null){
            httpEntity = new HttpEntity(this.httpHeaders);
        }
        else {
            httpEntity = new HttpEntity(this.body, this.httpHeaders);
        }
        return httpEntity;
    }

}
